﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SEO.Business;
using SEO.Entity;

public partial class DoDeductWeb : AdminPage
{
   
    /// <summary>
    /// ID
    /// </summary>
    protected int nID;

    /// <summary>
    /// 操作类型
    /// </summary>
    protected string strType = string.Empty;

    /// <summary>
    /// 新闻源操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        strType = Common.GetQueryValue("type", 0);
        int.TryParse(Common.GetQueryValue("id", 0), out nID);

        if (!Page.IsPostBack)
        {
            if (strType == "edit")
            {
                //页面数据初始化
                InitData();
            }
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.DeductWebList;
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitData()
    {
        DeductWeb Info = new BisDeductWeb().Get(nID);
        if (Info != null)
        {
            txtName.Text = Info.Name;
            txtUrl.Text = Info.Url;
            txtLinkFirstMomey.Text = Info.LinkFirstMomey.ToString();
            txtLinkSecondMomey.Text = Info.LinkSecondMomey.ToString();
        }
    }

    /// <summary>
    /// 提交操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string strUrl = txtUrl.Text.Trim().ToLower();
        strUrl = strUrl.Replace("http://wwww.", "");
        strUrl = strUrl.Replace("http://", "");
        strUrl = strUrl.Replace("/", "");
        BisDeductWeb bisInfo = new BisDeductWeb();
        if (string.IsNullOrEmpty(strUrl))
        {
            Until.Alert("请输入网址!");
            return;
        }
        //strUrl = Common.GetTopDomain(strUrl);
        if(bisInfo.CheckDeductWeb(nID,strUrl)){
             Until.Alert("网址已存在!");
            return;
        }
       
        DeductWeb Info = new DeductWeb();
        Info.Name = txtName.Text.Trim();
        Info.Url = strUrl;
        double dLinkFirstMomey = 0;
        double dLinkSecondMomey = 0;
        double.TryParse(txtLinkFirstMomey.Text.Trim(), out dLinkFirstMomey);
        double.TryParse(txtLinkSecondMomey.Text.Trim(), out dLinkSecondMomey);
        Info.LinkFirstMomey = dLinkFirstMomey;
        Info.LinkSecondMomey = dLinkSecondMomey;
        bool bResult = false;
        if (strType == "edit")
        {
            Info.ID = nID;
            bResult = bisInfo.Update(Info);
        }
        else
        {
            bResult = bisInfo.Insert(Info);
        }
        //结果处理
        if (bResult)
        {
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }
    }
}